package com.carloha.finance.tool;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 文件操作相关
 * @author 草原狼
 * @date 2019-7-13
 */
public class CarlohaFileUtil {
	static final Logger LOG = LoggerFactory.getLogger(CarlohaFileUtil.class);
	public static final String  charset = "UTF-8";
	
	/**
	 * 利用BufferedReader读取
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readerFile(String path) throws IOException {		
		BufferedReader br = null;
			File file = new File(path);
			if (!file.exists() || file.isDirectory()) {
				throw new FileNotFoundException();
			}
			try(InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charset)){
				br = new BufferedReader(isr);
				String temp = null;
				StringBuffer sb = new StringBuffer();
				temp = br.readLine();
				while (temp != null) {
					sb.append(temp + " ");
					temp = br.readLine();
				}
				return sb.toString();
			}
	}
	
	/**
	 * 读取流为数组
	 * @param inStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] readInputStream(InputStream inStream) throws Exception {
	    try {
	        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	        byte[] buffer = new byte[1024];
	        int len = 0;
	        while ((len = inStream.read(buffer)) != -1) {
	            outStream.write(buffer, 0, len);
	        }
	        inStream.close();
	        return outStream.toByteArray();
	    }catch (Exception e){
	        e.printStackTrace();
	        throw new Exception(e);
	    }
	}

	
	/**
	 * 创建文件夹
	 * 
	 * @param path
	 */
	public static boolean createDir(String path) {
		File dir = new File(path);
		if (!dir.exists()) {
			return dir.mkdirs();
		}else{
			return false;
		}

	}
	
	/**
	 * 创建新文件
	 * 
	 * @param path
	 * @param filename
	 * @throws IOException
	 */
	public static boolean createFile(String path, String filename) throws IOException {
		File file = new File(path + "/" + filename);
		if (!file.exists()) {
			return file.createNewFile();
		}else {
			return false;
		}

	}
	
	/**
	 * 删除文件
	 * 
	 * @param path
	 * @param filename
	 */
	public static boolean delFile(String path, String filename) {
		File file = new File(path + "/" + filename);
		if (file.exists() && file.isFile()) {
			return file.delete();
		}else {
			return false;
		}

	}
	
	/**
	 * 删除目录
	 * 
	 * @param path
	 */
	public static void delDir(String path) {
		File dir = new File(path);
		if (dir.exists()) {
			File[] tmp = dir.listFiles();
			if(tmp == null) {
				return ;
			}
			for (int i = 0; i < tmp.length; i++) {
				if (tmp[i].isDirectory()) {
					delDir(path + "/" + tmp[i].getName());
				} else {
					boolean flag = tmp[i].delete();
					if(! flag) {
						continue;
					}
				}
			}
			boolean flag = dir.delete();
			if(flag) {
				System.out.println("删除文件夹成："+path);
			}
		}
	}
	
	/**
	 * 拷贝文件到数组中
	 * @param inputStream
	 * @return
	 */
	public static byte[] copy(InputStream inputStream) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = -1;
			byte[] buf = new byte[8192];			
			while((len = inputStream.read(buf)) != -1) {
				baos.write(buf, 0, len);
			}			
			return baos.toByteArray();	
		}catch(Exception e) {
			e.getMessage();
		}finally {
			cloe(inputStream);
		}		
		return null;
		
	}
	
	/**
	 * 关闭输入流
	 * @param inputStream
	 */
	public static void cloe(InputStream inputStream) {
		try {
			if(inputStream != null) {
				inputStream.close();	
			}			
		}catch(Exception e) {
			e.getMessage();
		}
	}
}
